package com.wuliupai.delivery.wuliupaidelivery.activity;

import android.view.MenuItem;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.display.RoundedBitmapDisplayer;
import com.wuliupai.delivery.wuliupaidelivery.R;
import com.wuliupai.delivery.wuliupaidelivery.bean.BankCardRegexEntity;
import com.wuliupai.delivery.wuliupaidelivery.bean.WalletEntity;
import com.wuliupai.delivery.wuliupaidelivery.net.NetResultOperate;
import com.wuliupai.delivery.wuliupaidelivery.utils.BitmapUtils;
import com.wuliupai.delivery.wuliupaidelivery.utils.JsonUtils;
import com.wuliupai.delivery.wuliupaidelivery.utils.LogUtils;
import com.wuliupai.delivery.wuliupaidelivery.utils.SharePreferenceUtil;

import org.androidannotations.annotations.AfterViews;
import org.androidannotations.annotations.Bean;
import org.androidannotations.annotations.Click;
import org.androidannotations.annotations.EActivity;
import org.androidannotations.annotations.UiThread;
import org.androidannotations.annotations.ViewById;

/**
 * 银行卡界面
 */
@EActivity(R.layout.activity_walle_bank_card)
public class WalleBankCard extends AppBaseActivity {

    @Bean(SharePreferenceUtil.class)
    SharePreferenceUtil sharePreferenceUtil;
    private int whetherBank;
    private int[] BankCardLogo = new int[]{ // 银行卡图标
            R.mipmap.logo_bh01,
            R.mipmap.logo_bh02,
            R.mipmap.logo_bh03,
            R.mipmap.logo_bh04,
            R.mipmap.logo_bh05,
            R.mipmap.logo_bh06,
            R.mipmap.logo_bh07,
            R.mipmap.logo_bh08,
            R.mipmap.logo_bh09,
            R.mipmap.logo_bh10,
            R.mipmap.logo_bh11,
            R.mipmap.logo_bh12,
            R.mipmap.logo_bh13,
            R.mipmap.logo_bh14,
            R.mipmap.logo_bh15,
            R.mipmap.logo_bh16,
            R.mipmap.logo_bh17,
            R.mipmap.logo_bh18,
            R.mipmap.logo_bh19,
            R.mipmap.logo_bh20,
            R.mipmap.logo_bh21,
    };


    private BankCardRegexEntity bankCardRegexEntity; //银行卡正则实体类
    private String bankCardNum; //银行卡号
    private String bankCardName; //持卡人姓名

    @ViewById(R.id.fl_walle_bank_card_add)
    FrameLayout fl_walle_bank_card_add;//添加银行卡按钮

    @ViewById(R.id.lv_walle_bank_card)
    LinearLayout lv_walle_bank_card;//银行卡

    @ViewById(R.id.iv_walle_bank_card_icon)
    ImageView iv_walle_bank_card_icon;//显示银行卡发卡银行logo

    @ViewById(R.id.tv_bank_card_name)
    TextView tv_bank_card_name;//发卡银行名称

    @ViewById(R.id.tv_bank_card_type)
    TextView tv_bank_card_type;//银行卡类型

    @ViewById(R.id.tv_bank_card_num_digit)
    TextView tv_bank_card_num_digit;//银行卡占位符 *

    @ViewById(R.id.tv_bank_card_num_mantissa)
    TextView tv_bank_card_num_mantissa;//银行卡后4位


    @Click(R.id.fl_walle_bank_card_add)
    void addBankCard() {//点击添加银行卡
        //跳转添加银行卡界面
        WalletAddBankCard_.intent(this).pagerTitle(0).start();
    }

    @AfterViews
    void startPage() {
        setDefaultTitle(this, R.string.strings_my_info_wallet_my_bank_card);
    }

    @Override
    protected void onStart() {
        super.onStart();
        initViews();
        loadDatas();
    }

    /**
     * 显示添加银行卡按钮
     */
    @UiThread
    void showAddBankCard(int whetherBank) {
        if (whetherBank == 0) {
            //显示添加银行卡按钮 隐藏银行卡
            fl_walle_bank_card_add.setVisibility(View.VISIBLE);
            lv_walle_bank_card.setVisibility(View.GONE);
        } else if (whetherBank == 1) {
            //显示银行卡 隐藏添加银行卡按钮
            lv_walle_bank_card.setVisibility(View.VISIBLE);
            fl_walle_bank_card_add.setVisibility(View.GONE);
        }

    }

    /**
     * 加载数据
     */
    private void loadDatas() {

        WalletEntity walletEntity = new WalletEntity(this);
        String walletEntityJson = JsonUtils.createJsonString(walletEntity);
        LogUtils.log("上传 获取用户的银行卡信息" + walletEntityJson);
        try {
            String aes = JsonUtils.aes(walletEntityJson);
            callNet(this, "getuserbankinfo", aes, new NetResultOperate() {
                @Override
                public void onSuccess(String content) {
                    LogUtils.log("获取用户的银行卡信息" + content);

                    WalletEntity walletEntity = (WalletEntity) JsonUtils.parseJsonString(content, WalletEntity.class);
                    if (!content.equals("{}")) {
                        showAddBankCard(whetherBank);
                        //获取银行卡对应发卡银行
                        int bnakNamePosition = Integer.parseInt(walletEntity.getBankId());
                        String[] BankCardNames = getResources().getStringArray(R.array.BankCardName);
                        tv_bank_card_name.setText(BankCardNames[bnakNamePosition - 1]);
                        ImageLoader.getInstance().displayImage("drawable://" + BankCardLogo[bnakNamePosition - 1], iv_walle_bank_card_icon, getOptions());

                        //设置银行卡类型
                        tv_bank_card_type.setText("储蓄卡");

                        // 获取持卡人姓名
                        bankCardName = walletEntity.getCardName();

                        //获取银行卡号 解析并显示
                        bankCardNum = walletEntity.getCardNumber();
                        parserBankCardNum(bankCardNum);
                    }
                }

                @Override
                public void onDataEmpty(String content) {
                }

                @Override
                public void onDataEnd(String content) {
                }
            });
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 获取ImageLoader的DisplayImageOptions
     *
     * @return
     */
    public DisplayImageOptions getOptions() {
        // 设置图片缓存的目录
        BitmapUtils.setImageLoaderCache(this);

        return new DisplayImageOptions.Builder()
                .showImageOnLoading(R.drawable.rect_approve_transparency)// 设置图片在下载期间显示的图片
                .showImageForEmptyUri(R.drawable.rect_approve_transparency)// 设置图片URI为空或是错误的时候显示的图片
                .showImageOnFail(R.drawable.rect_approve_transparency)// 设置图片加载错误时显示的图片
                .cacheInMemory(false)// 设置下载的图片是否缓存在内存中
                .cacheOnDisc(false)// 设置下载的图片是否缓存在SD卡中
                .cacheOnDisk(false)
                .displayer(new RoundedBitmapDisplayer(90))
                .build();
    }

    /**
     * 初始化界面
     */
    private void initViews() {

        whetherBank = sharePreferenceUtil.getWhetherBank();
        showAddBankCard(whetherBank);

        if (whetherBank == 0) {
            setDefaultTitle(this, R.string.strings_my_info_wallet_my_bank_card);
        } else if (whetherBank == 1) {
            setTitle(this, R.string.strings_my_info_wallet_my_bank_card, R.drawable.comm_press_return_selector, true, new OnNavigationBtnClickListener() {
                @Override
                public void navigationClick() {
                    finish();
                    animUtils.leftOutAnimation(WalleBankCard.this);
                }

                @Override
                public void menuItemClick(MenuItem item) {
                    switch (item.getItemId()) {
                        case R.id.menuItem1:
                            //WalletAddBankCard_.intent(WalleBankCard.this).pagerTitle(1).extra("bankCardNum", bankCardNum).start();
                            break;
                    }
                }
            });
        }
    }

    /**
     * 设置title右侧按键样式
     *//*
    @Override
    public boolean onPrepareOptionsMenu(Menu menu) {
        if (whetherBank == 1){
            menu.findItem(R.id.action_share).setVisible(true).setTitle("修改银行卡");
            menu.findItem(R.id.action_edit).setVisible(false);
            return true;
        }
        return false;
    }*/

    /**
     * 解析银行卡号 设置银行卡占位符和银行卡尾数
     */
    private void parserBankCardNum(String bankCardNum) {

        int placeholderLength = bankCardNum.length() - 4;

        //生成占位符
        String placeholder = "";
        for (int i = 1; i <= placeholderLength; i++) {
            placeholder = placeholder + "*";
            if (i % 4 == 0) {
                placeholder = placeholder + " ";
            }
        }
        String[] split = placeholder.split(" ");
        String subPlaceholder = split[split.length - 1];

        //如果占位符最后不足4位 用尾数填充
        int FillDigit = 0;
        if (subPlaceholder.length() != 4) {
            FillDigit = 4 - subPlaceholder.length();
        }

        String subBackCardNum = bankCardNum.substring(bankCardNum.length() - 4, bankCardNum.length());
        StringBuilder stringBuilder = new StringBuilder(subBackCardNum);
        stringBuilder.insert(FillDigit, " ");

        // 设置显示银行卡号
        tv_bank_card_num_digit.setText(placeholder);
        tv_bank_card_num_mantissa.setText(stringBuilder.toString());

    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
    }
}
